package com.one.common.view.preview;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.R;
import com.one.common.config.RouterPath;
import com.one.common.view.base.BaseActivity;

import java.util.ArrayList;

/**
 * creator: zhulunjun
 * time:    2018/8/20
 * describe:
 */
@Route(path = RouterPath.PRE_PIC)
public class PreviewPicActivity extends BaseActivity implements PhotoPagerAdapter.PhotoViewClickListener {


    public static final String EXTRA_PHOTOS = "extra_photos";
    public static final String EXTRA_CURRENT_ITEM = "extra_current_item";


    private ArrayList<String> paths;
    private ViewPagerFixed mViewPager;
    private PhotoPagerAdapter mPagerAdapter;
    private int currentItem = 0;
    private ImageView iv_back;


    @Override
    protected void initPresenter() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_preview;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        noHasTitle();
    }

    @Override
    public void initView() {
        super.initView();
        mViewPager = (ViewPagerFixed) findViewById(R.id.vp_photos);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        paths = (ArrayList<String>) getIntent().getSerializableExtra(EXTRA_PHOTOS);
        currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0);
        mPagerAdapter = new PhotoPagerAdapter(this, paths);
        mPagerAdapter.setPhotoViewClickListener(this);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(currentItem);
        mViewPager.setOffscreenPageLimit(5);
    }


    @Override
    public void OnPhotoTapListener(View view, float v, float v1) {
        onBackPressed();
    }


    public static void starPreview(Context context,ArrayList<String> path, int cur){
        Intent intent = new Intent(context, PreviewPicActivity.class);
        intent.putStringArrayListExtra(EXTRA_PHOTOS, path);
        intent.putExtra(EXTRA_CURRENT_ITEM, cur);
        context.startActivity(intent);
    }

    public static void starSinglePreview(Context context,String path){
        ArrayList<String> urls = new ArrayList<>();
        urls.add(path);
        Intent intent = new Intent(context, PreviewPicActivity.class);
        intent.putStringArrayListExtra(EXTRA_PHOTOS, urls);
        intent.putExtra(EXTRA_CURRENT_ITEM, 0);
        context.startActivity(intent);
    }
}
